package cn.itcast.user.web;

import cn.itcast.user.config.PatternProperties;
import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    @Autowired
    private UserService userService;
    @Value("${pattern.dateformat}")
    private String dateformat;

    @GetMapping("/now")
    public String now() {

        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));
    }

    @Autowired
    private PatternProperties patternProperties;

    @GetMapping("/now2")
    public String now2() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternProperties.getDateformat()));
    }

    @Value("${look.baimingdan}")
    private String baimingdan;

    @GetMapping("/look/{ip}")
    public String baimingdan(@PathVariable("ip") String ip) {

        String[] split = baimingdan.split(",");
        List<String> list = Arrays.asList(split);
        if (!list.contains(ip)) {
            return "不在白名单中，滚犊子~~~";
        }
        return "在白名单中，可以正常访问图片资源";
    }

    @GetMapping("/prop")
    public PatternProperties prop(){
        return patternProperties;
    }

    @Value("${server.port}")
    String port;
    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/queryById/{id}")
    public User queryById(@PathVariable("id") Long id) {
        User user = userService.queryById(id);
        user.setAddress(user.getAddress()+":"+port);
        return user;
    }

    @GetMapping("/one/{id}")
    public User queryById2(@PathVariable("id") Long id, String username, @RequestHeader(value = "Truth") String truth) {
        System.out.println("Truth"+truth);
        User user = userService.queryById(id);
        user.setUsername(username + ": " + truth);
        return user;
    }

}
